<template>
  <div>
    <UploadExcel :on-success="upload" />
  </div>
</template>

<script>
import { importEmployee } from "@/api/employees";
export default {
  methods: {
    async upload(data) {
      // console.log(data.results);
      const newRseults = data.results.map((user) => {
        return this.zh2en(user);
      });
      await importEmployee(newRseults);
      this.$message.success("导入成功");
      this.$router.back();
    },
    zh2en(user) {
      const newUser = {};
      const dict = {
        入职日期: "timeOfEntry",
        手机号: "mobile",
        姓名: "username",
        转正日期: "correctionTime",
        工号: "workNumber",
      };
      for (const key in user) {
        //key为导入员工的中文key
        //dick[key]是将中文key拿到字典里去找到翻译
        //最后将翻译出来的英文key去赋值 值为旧对象里的key的val
        //最后的newUser为翻译出来的英文对象 最后以英文对象的形式传给后端
        const enkey = dict[key];
        if (enkey === "timeOfEntry" || enkey === "correctionTime") {
          newUser[enkey] = new Date(this.formatDateExcel(user[key], "/"));
        } else {
          newUser[enkey] = user[key];
        }
      }
      return newUser;
    },
    formatDateExcel(num, format) {
      num = Number(num); // 强制类型转化，以防传来的值是字符串
      let millisecond = 0; // 转化后的毫秒数
      if (num > 60) {
        millisecond = (num - 25569) * 60 * 60 * 24 * 1000;
      } else {
        millisecond = (num - 25568) * 60 * 60 * 24 * 1000;
      }
      let date = new Date(millisecond); // 根据转化后的毫秒数获取对应的时间
      let yy = date.getFullYear();
      let mm =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1;
      let dd = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      return yy + format + mm + format + dd; // 返回格式化后的日期
    },
  },
};
</script>

<style>
</style>